<?php

class Preview extends CI_Controller {

	public function __construct() {
		parent::__construct();

		$this->load->helper(array('form','url'));
		$this->load->library('form_validation');
		$this->load->model('paper_model');
		//echo "hello world!";
	}

	public function parseWord($userDoc) {                                                   
		$fileHandle = fopen ( $userDoc, "r" );                                              
		$line = @fread ( $fileHandle, filesize ( $userDoc ) );                              
		$lines = explode ( chr ( 0x0D ), $line );                                           
		$outtext = "";                                                                      
		foreach ( $lines as $thisline ) {                                                   
			$pos = strpos ( $thisline, chr ( 0x00 ) );                                      
			if (($pos !== FALSE) || (strlen ( $thisline ) == 0)) {                          
			} else {                                                                        
				$outtext .= $thisline . " ";                                                
			}                                                                               
		}                                                                                   
		//  $outtext = preg_replace ( "/[^a-zA-Z0-9\s\,\.\-\n\r\t@\/\_\(\)]/", "", $outtext );
		return $outtext;                                                                    
	} 

	public function index($file_name) {
		//check suffix
		$suffix = strtolower(pathinfo($file_name,PATHINFO_EXTENSION));
		$file_name = urldecode($file_name);
		$file_name= html_entity_decode($file_name);
		$file_name= '"'.$file_name.'"';
		$paper = array();

		log_message("debug",'file_namefile_name'.$file_name);
		if($suffix =="txt")
		{
			header("Content-Type: text/html; charset=GB-2312");
			$paper = file("/data/papers/file/$file_name");

		}
		else if(($suffix =="wps")||($suffix == "doc"))
		{
			//url decode??
			header("Content-Type: text/html; charset=UTF-8");
			$file_name= "/data/papers/file/$file_name";

			$cmd = "antiword -m UTF-8.txt $file_name ";
			//$cmd = "catdoc -m128 -d utf-8 $file_name";
			exec($cmd,$paper);
		}
		else
		{	
			header("Content-Type: text/html; charset=UTF-8");
			echo "对不起！暂不支持该类型文档的预览";
			exit;
		}

		$num = count($paper);
		if($num == 0)
		{
			echo "文档读取失败!";
			exit;
		}

		//固定显示前200行
		$num = ($num<300?$num:300);
		for($i = 0; $i < $num; $i++)
		{
			echo $paper[$i]."<br />";
		}

	}

}

